<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/7/18 0018
 * Time: 16:14
 */
namespace App\Admin\Controllers;
use App\Admin\Contracts\ActionInterface;
use App\Base\Controllers\Controller;
use Illuminate\Http\Request;

/**
 * 用户行为
 * Class ActionController
 * @package App\Admin\Controllers
 */
class ActionController extends Controller
{

    /**
     * 行为操作日志
     * @return ActionInterface
     */
    public function getActionInterface(){
        return app()->make(ActionInterface::class);
    }
    /**
     * 用户行为
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request){
        if ($request->getMethod() == "POST") {
        $where=function($query) use ($request){
           $name=$request->input("name");
           if(!empty($name)){
               $query->where("name",$name);
           }
           $title=$request->input("title");
           if(!empty($title)){
               $query->where("title","like",'%'.$title.'%');
           }
        };
          return $this->getActionInterface()->getActionList($where);
        }
        return view("admin.action.index");
    }

    /**
     * 日志行为添加
     * @param Request $request
     */
    public function add(Request $request){
        if ($request->getMethod() == "POST") {
            return $this->getActionInterface()->updateOrCreateByData($request->all());
        }
        return view("admin.action.add");
    }

    /**
     * 日志行为添加
     * @param Request $request
     */
    public function edit(Request $request){
        if ($request->getMethod() == "POST") {
            return $this->getActionInterface()->updateOrCreateByData($request->all());
        }
        $id=$request->input("id");
        $info = $this->getActionInterface()->getActionInfo(["id"=>$id]);
        return view("admin.action.edit")->with("info",$info);
    }

    /**
     * 删除行为
     * @param Request $request
     * @return mixed
     */
    public function del(Request $request){
        return $this->getActionInterface()->delActionInfo($request->all());
    }
    /**
     * 用户行为日志表
     * @param Request $request
     * @return mixed
     */
    public function logIndex(Request $request){
        if ($request->getMethod() == "POST") {
            $where=function($query) use ($request){
                $action_id=$request->input("action_id");
                if(!empty($action_id)){
                    $query->where("id",$action_id);
                }

            };
            return $this->getActionInterface()->getActionLogByActionList($where,$request->all());
        }
        $actionList=$this->getActionInterface()->getActionList([]);
        return view("admin.action.log_index")->with("actionList",$actionList);

    }

    /**
     * 行为日志删除
     * @param Request $request
     */
    public function logDel(Request $request){
        return $this->getActionInterface()->delActionLogInfo($request->all());
    }

}